﻿using UnityEngine;
using System.Collections;

public partial class TankCitizenFSM 
{
    //-----------------------------------CloseAtkState------------------------------------------
    IEnumerator CloseAtkState()
    {
        float fDmgApplyTime = 0.0f; //Damage Delay


        gameObject.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.CLOSEATTACK);
        //gameObject.transform.GetChild(2).gameObject.active = false;

        //gameObject.GetComponent<PolyNavAgent>().Stop();

        Calculate.RotToDest(gameObject, m_ObjCloseAtkTarget.transform.position);

        do
        {
            yield return null;

            if (CommonStateChange())
                break;

            fDmgApplyTime += Time.deltaTime;

            if (fDmgApplyTime >= gameObject.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).length) //if Animroof End, Apply Dmg
            {
                if (m_ObjCloseAtkTarget == null)
                {
                    gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.MOVE;
                    break;
                }
                else
                {
                    m_ObjCloseAtkTarget.GetComponent<UnitStatus>().m_unitData.iCurrentHP -= 2;
                    m_ObjCloseAtkTarget.GetComponent<UnitStatus>().m_unitData.iCurrentMorale -= 3;
                    m_ObjCloseAtkTarget.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.DAMAGE;
                    Calculate.RotToDest(m_ObjCloseAtkTarget, gameObject.transform.position);
                    fDmgApplyTime = 0.0f;
                }

            }



        } while (gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.CLOSEATTACK);

        SetState(gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState);
    }

    IEnumerator TraceState()
    {
        gameObject.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.MOVE);

        Calculate.RotToDest(gameObject, m_ObjTarget.transform.position);
        m_vecDest = m_ObjTarget.transform.position;
        //gameObject.GetComponent<PolyNavAgent>().SetDestination(m_vecDest);

        do
        {
            yield return null;

            if (CommonStateChange())
                break;

            if (m_ObjTarget == null)
            {
                gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.MOVE;
                break;
            }

            Calculate.RotToDest(gameObject, m_ObjTarget.transform.position);
            m_vecDest = m_ObjTarget.transform.position;
            //gameObject.GetComponent<PolyNavAgent>().SetDestination(m_vecDest);

            CloseAtkEnable();
            AimEnable();



        } while (gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.TRACE);

        SetState(gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState);
    }

    IEnumerator ThrowState()
    {
        float m_fThrowTime = 0.0f;

        gameObject.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.THROW);
        //gameObject.GetComponent<PolyNavAgent>().Stop();

        Calculate.RotToDest(gameObject, m_ObjTarget.transform.position);

        do
        {
            yield return null;

            m_fThrowTime += Time.deltaTime;

            Calculate.RotToDest(gameObject, m_ObjTarget.transform.position);

            if (m_fThrowTime >= gameObject.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).length - 0.1f)
            {
                gameObject.GetComponent<UnitStatus>().m_WeaponData.m_iCurrentAmmo -= 1;


                //Accur Chk

                int iMyAccur = gameObject.GetComponent<UnitStatus>().m_unitData.iAccur;

                if (m_ObjTarget.GetComponent<UnitStatus>().m_unitData.m_bCoverd == true)
                {
                    iMyAccur -= m_ObjTarget.GetComponent<UnitStatus>().m_unitData.m_Cover.GetComponent<BuildStatus>().m_BuildData.iCover;
                }

                int iRandAccurVal = Random.Range(0, 10);

                Vector3 BulletPos;
                bool bHitFlag = false;

                if (iRandAccurVal <= iMyAccur)
                {
                    BulletPos = m_ObjTarget.transform.position;
                    bHitFlag = true;
                }
                else
                {
                    float fRandX = Random.Range(-0.2f, 0.2f);
                    float fRandY = Random.Range(-0.2f, 0.2f);

                    BulletPos = new Vector3(m_ObjTarget.transform.position.x + fRandX, m_ObjTarget.transform.position.y + fRandY, m_ObjTarget.transform.position.z);
                }



                ObjFactory.CreateThrow(gameObject.transform.position, BulletPos, bHitFlag);

                m_fThrowTime = 0.0f;

                gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.MOVE;

                break;
            }

        } while (gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.THROW);

        SetState(gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState);
    }

    IEnumerator TauntState()
    {
        float m_fTauntTime = 0.0f;

        gameObject.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.TAUNT);
        //gameObject.GetComponent<PolyNavAgent>().Stop();
        Calculate.RotToDest(gameObject, m_ObjTarget.transform.position);

        do
        {
            yield return null;

            m_fTauntTime += Time.deltaTime;

            if (m_fTauntTime >= gameObject.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).length)
            {
                gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.MOVE;
                m_fTauntTime = 0.0f;
                break;
            }

        } while (gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.TAUNT);

        SetState(gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState);

    }
}
